package com.hmkj.admin.controller.order;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.entity.export.ExportUtils;
import com.hmkj.core.constant.Constants;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.investor.InvestorModel;
import com.hmkj.core.model.order.OrderSimulateModel;
import com.hmkj.core.po.investor.InvestorSimulate;
import com.hmkj.core.po.order.Order;
import com.hmkj.core.po.order.OrderSimulate;
import com.hmkj.core.po.system.Admin;
import com.hmkj.core.service.investor.InvestorSimulateService;
import com.hmkj.core.service.order.OrderSimulateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class OrderSimulateJsonController extends GenericController {

    @Resource
    private OrderSimulateService orderSimulateService;
    @Resource
    private InvestorSimulateService investorSimulateService;


    /**
     * 模拟策略列表json数据
     */
    @ResponseBody
    @RequestMapping(value = "orderSimulate/json/list", method = RequestMethod.POST)
    @RequiresPermissions("sys:orderSimulate:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
        DtGrid dtgrid = dataAuthority(dtGridPager, null);
        return orderSimulateService.getDtGridList(dtgrid, OrderSimulateModel.class);
    }

    /**
     * 接单
     */
    @ResponseBody
    @RequestMapping(value = "orderSimulate/json/taking", method = RequestMethod.POST)
    @RequiresPermissions("sys:orderSimulate:taking")
    public ResultEntity taking(Integer id) throws Exception {
        Admin admin = AdminSessionHelper.getCurrAdmin();
        //1.模拟订单状态等基础校验
        OrderSimulate orderSimulate = checkSimulateOrder(id,"taking");
        orderSimulateService.takingOrder(orderSimulate);
        return ok();
    }

    /**
     * 流单
     */
    @ResponseBody
    @RequestMapping(value = "orderSimulate/json/flow", method = RequestMethod.POST)
    @RequiresPermissions("sys:orderSimulate:flow")
    public ResultEntity flow(Integer id) throws Exception {
        //1.模拟订单状态等基础校验
        OrderSimulate orderSimulate = checkSimulateOrder(id,"flow");
        //2.退还点买人履约保证金，退还点买人手续费，退还投资人止盈保证金，
        orderSimulateService.flowOrder(orderSimulate);
        return ok();
    }

    /**
     * 撤单
     */
    @ResponseBody
    @RequestMapping(value = "orderSimulate/json/cancel", method = RequestMethod.POST)
    @RequiresPermissions("sys:orderSimulate:cancel")
    public ResultEntity cancel(Integer id) throws Exception {
        //1.模拟订单状态等基础校验
        OrderSimulate orderSimulate = checkSimulateOrder(id,"cancel");
        orderSimulateService.cancelOrder(orderSimulate);
        return ok();
    }

    /**
     * 平仓
     */
    @ResponseBody
    @RequestMapping(value = "orderSimulate/json/cover", method = RequestMethod.POST)
    @RequiresPermissions("sys:orderSimulate:cover")
    public ResultEntity cover(Integer id) throws Exception {
        //1.模拟订单状态等基础校验
        OrderSimulate orderSimulate = checkSimulateOrder(id,"cover");
        orderSimulateService.coverOrderSimulate(orderSimulate,orderSimulate.getNumber(),OrderSimulate.SETTLETYPE.T5.code);
        return ok();
    }

    /**
     * 模拟订单操作基础校验
     * @param id
     */
    OrderSimulate checkSimulateOrder(Integer id,String option){
        //获取当前登录用户检验是否为投资人
        Admin admin = AdminSessionHelper.getCurrAdmin();
//        if(admin.getType().intValue() != Admin.TYPE.T3.code)throw new BussinessException("该操作只允许代理商操作");
        if(null == id){
            throw new BussinessException("模拟订单参数有误");
        }
        OrderSimulate orderSimulate = orderSimulateService.selectByPrimaryKey(id);
        if(null == orderSimulate){
            throw new BussinessException("模拟订单不存在");
        }
        if("taking".equals(option)){//接单
            if(orderSimulate.getStatus() != OrderSimulate.STATUS.T1.code){
                throw new BussinessException("模拟订单状态已更改，接单操作失败");
            }
        }else if("flow".equals(option)){
            if(orderSimulate.getStatus() != Order.STATUS.T1.code){
                throw new BussinessException("模拟订单状态已更改，流单操作失败");
            }
        }else if("cancel".equals(option)){
            if(orderSimulate.getStatus() != Order.STATUS.T2.code && orderSimulate.getStatus() != Order.STATUS.T4.code){
                throw new BussinessException("模拟订单状态已更改，撤单操作失败");
            }
        }else if("cover".equals(option)){
            if(orderSimulate.getStatus() != Order.STATUS.T3.code && orderSimulate.getStatus() != Order.STATUS.T4.code){
                throw new BussinessException("模拟订单状态已更改，平仓操作失败");
            }
        }
        return orderSimulate;
    }


    /**
     * 列表导出
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "orderSimulate/json/export", method = RequestMethod.POST)
    public ResultEntity export(String dtGridPager, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 执行导出
        DtGrid dtgrid = dataAuthority(dtGridPager, null);
        dtgrid  =  orderSimulateService.getDtGridListExport(dtgrid, InvestorModel.class);
        ExportUtils.export(request, response,dtgrid);
        return ok();
    }

}